package PL.practica10;

import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import org.antlr.runtime.tree.CommonTree;

/**
 * @author Jorg Werner
 */
public class ASTToTreeModelAdapter implements TreeModel {

        CommonTree m_tree = null;

        public ASTToTreeModelAdapter(CommonTree t) {
                m_tree = t;
        }

        /**
         * @see javax.swing.tree.TreeModel#addTreeModelListener(javax.swing.event.
         *      TreeModelListener)
         */
        public void addTreeModelListener(TreeModelListener l) {

        }

        /**
         * @see javax.swing.tree.TreeModel#getChild(java.lang.Object, int)
         */
        public Object getChild(Object parent, int index) {
                return ((CommonTree) parent).getChild(index);
        }

        /**
         * @see javax.swing.tree.TreeModel#getChildCount(java.lang.Object)
         */
        public int getChildCount(Object parent) {
                return ((CommonTree) parent).getChildCount();
        }

        /**
         * @see javax.swing.tree.TreeModel#getIndexOfChild(java.lang.Object,
         *      java.lang.Object)
         */
        public int getIndexOfChild(Object parent, Object child) {
                if (parent == null || child == null)
                        return -1;
                CommonTree cp = ((CommonTree) parent);
                CommonTree cc = ((CommonTree) child);
                for (int i = 0; i < cp.getChildCount(); i++) {
                        if (cp.getChild(i).equals(cc))
                                return i;
                }
                return -1;
        }

        /**
         * @see javax.swing.tree.TreeModel#getRoot()
         */
        public Object getRoot() {
                return m_tree;
        }

        /**
         * @see javax.swing.tree.TreeModel#isLeaf(java.lang.Object)
         */
        public boolean isLeaf(Object node) {
                CommonTree cn = ((CommonTree) node);
                boolean r = (cn.getChildCount() == 0) ? true : false;
                return r;
        }

        /**
         * @see javax.swing.tree.TreeModel#removeTreeModelListener(javax.swing.event.
         *      TreeModelListener)
         */
        public void removeTreeModelListener(TreeModelListener l) {

        }

        /**
         * @see javax.swing.tree.TreeModel#valueForPathChanged(javax.swing.tree.TreePath,
         *      java.lang.Object)
         */
        public void valueForPathChanged(TreePath path, Object newValue) {

        }

}